package com.salim.leetcode.util;

import java.util.*;

public class TreeNodeUtils {
    public static TreeNode generateTreeNode(Integer[] array){
        if(array.length==0){
            return null;
        }
        Integer first = array[0];
        TreeNode root = new TreeNode(first);
        List<TreeNode> currentLevel = new ArrayList<>();
        currentLevel.add(root);
        int currentIndex = 0;
        int arrayIndex = 1;
        while (currentLevel.size()<array.length){
            TreeNode current = currentLevel.get(currentIndex);
            if(current==null){
                continue;
            }
            if(arrayIndex<array.length){
                if(array[arrayIndex] == null){
                    currentLevel.add(null);
                }else{
                    TreeNode left = new TreeNode(array[arrayIndex]);
                    currentLevel.add(left);
                    current.left = left;
                }
                arrayIndex++;
            }
            if(arrayIndex<array.length){
                if(array[arrayIndex] == null){
                    currentLevel.add(null);
                }else{
                    TreeNode right = new TreeNode(array[arrayIndex]);
                    currentLevel.add(right);
                    current.right = right;
                }
                arrayIndex++;
            }
            currentIndex++;
        }
        return root;
    }

    public static void main(String[] args){
        Integer[] arr = {2,3,3,4,5,5,4,null,null,8,9,9,8};
        TreeNode root = generateTreeNode(arr);
        TreeNode left = root.left;
    }
}
